class Solution {
public:
    int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
        vector<vector<int>> net(m, vector<int>(n, 1));
        for (auto& wall : walls) {
            net[wall[0]][wall[1]] = -1;
        }
        for (auto& guard : guards) net[guard[0]][guard[1]] = -1;
        for (auto& guard : guards) {
            int x = guard[0], y = guard[1];
            for (int a = x, b = y - 1; a >= 0 && a < m && b >= 0 && b < n; b--) {
                if (net[a][b] == -1) break;
                net[a][b] = 0;
            }
            for (int a = x, b = y + 1; a >= 0 && a < m && b >= 0 && b < n; b++) {
                if (net[a][b] == -1) break;
                net[a][b] = 0;
            }
            for (int a = x - 1, b = y; a >= 0 && a < m && b >= 0 && b < n; a--) {
                if (net[a][b] == -1) break;
                net[a][b] = 0;
            }
            for (int a = x + 1, b = y; a >= 0 && a < m && b >= 0 && b < n; a++) {
                if (net[a][b] == -1) break;
                net[a][b] = 0;
            }
        }
        int res = 0;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) if (net[i][j] == 1) res++;

        return res;
    }
};